package com.kaili.basic.service.impl;

/**
 * Created by xxx on 2019-03-07.
 */

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.CustomerAddressMapper;
import com.kaili.basic.mapper.CustomerMapper;
import com.kaili.basic.pojo.dto.CustomerAddDto;
import com.kaili.basic.pojo.dto.CustomerAddressDto;
import com.kaili.basic.pojo.dto.CustomerQueryDto;
import com.kaili.basic.pojo.dto.CustomerUpdateDto;
import com.kaili.basic.pojo.entity.*;
import com.kaili.basic.service.CustomerService;
import com.kaili.common.util.LoginDetailUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author xxx
 * @Copyright: Copyright (c) 2017
 * @Company: 杭州凯立通信有限公司
 */
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    CustomerAddressMapper customerAddressMapper;

    @Override
    public int addCustomer(CustomerAddDto customerAddDto) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerAddDto, customer);
        Long customerId = customerMapper.getMaxCustomerId();
        customerId = customerId + 1L;
        if(customer.getCustomerCode()==null || "".equals(customer.getCustomerCode())) {
            customer.setCustomerCode(StringUtils.leftPad(customerId.toString(), 5, "0"));
        }
        customer.setCustomerId(customerId);
        customer.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        customerMapper.addCustomer(customer);
        int order=1;
        for (CustomerAddressDto customerAddressDto:customerAddDto.getCustomerAddressList()) {
            CustomerAddress customerAddress = new CustomerAddress();
            BeanUtils.copyProperties(customerAddressDto, customerAddress);
            customerAddress.setCreateBy(customer.getCreateBy());
            customerAddress.setCustomerId(customer.getCustomerId());
            customerAddress.setCustomerAddressOrder(order);
            customerAddressMapper.addCustomerAddress(customerAddress);
            order++;
        }
        return 1;
    }

    @Override
    public int deleteCustomer(Customer customer) {
        customerMapper.deleteCustomer(customer);
        CustomerAddress customerAddress=new CustomerAddress();
        customerAddress.setCustomerId(customer.getCustomerId());
        customerAddress.setModifyBy(customer.getModifyBy());
        customerAddressMapper.deleteCustomerAddress(customerAddress);
        return 1;
    }

    @Override
    public int deleteCustomers(Map<String,Object> map) {
        customerMapper.deleteCustomers(map);
        customerAddressMapper.deleteCustomerAddressList(map);
        return 1;
    }

    @Override
    public int updateCustomer(CustomerUpdateDto customerUpdateDto) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerUpdateDto, customer);
        customer.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        customerMapper.updateCustomer(customer);
        CustomerAddress customerAddress=new CustomerAddress();
        customerAddress.setCreateBy(customer.getModifyBy());
        customerAddress.setCustomerId(customer.getCustomerId());
        customerAddress.setModifyBy(customer.getModifyBy());
        customerAddressMapper.deleteCustomerAddress(customerAddress);
        int order=1;
        for (CustomerAddressDto customerAddressDto:customerUpdateDto.getCustomerAddressList()) {
            CustomerAddress customerAddressMy = new CustomerAddress();
            BeanUtils.copyProperties(customerAddressDto, customerAddressMy);
            customerAddressMy.setCreateBy(customer.getModifyBy());
            customerAddressMy.setCustomerId(customer.getCustomerId());
            customerAddressMy.setModifyBy(customer.getModifyBy());
            customerAddressMy.setCustomerAddressOrder(order);
            customerAddressMapper.addCustomerAddress(customerAddressMy);
            order++;
        }
        return 1;
    }

    @Override
    public Page<CustomerPlusAddress> queryCustomer(Page<CustomerPlusAddress> page,CustomerPlusAddress customer) {
        List<CustomerPlusAddress> customers = customerMapper.queryCustomer(page,customer);
        return page.setRecords(customers);
    }

    @Override
    public Customer queryCustomerById(Long customerId) {
        return customerMapper.queryCustomerById(customerId);
    }

    @Override
    public List<Customer> queryCustomerBySupplier(Long supplierId) {
        return customerMapper.queryCustomerBySupplier(supplierId);
    }

    @Override
    public List<Customer> dimQueryCustomerBySupplier(Map<String,Object> map) {
        return customerMapper.dimQueryCustomerBySupplier(map);
    }

    @Override
    public List<CustomerAllQuery> queryCustomerType(Integer customerType) {
        return customerMapper.queryCustomerType(customerType);
    }

    @Override
    public List<CustomerAllQuery> queryAllCustomer() {
        return customerMapper.queryAllCustomer();
    }

    @Override
    public Long getMaxCustomerId() {
        return customerMapper.getMaxCustomerId();
    }

    @Override
    public List<CustomerExport> queryCustomerExport(CustomerQueryDto customerQueryDto) {
        return customerMapper.queryCustomerExport(customerQueryDto);
    }
}
